//
//  HotNewsView.m
//  NXYHotNews
//
//  Created by 聂晓昀 on 2017/12/28.
//  Copyright © 2017年 NXY. All rights reserved.
//

#import <UIKit/UIKit.h>

/**
 * 自定义cell 展示滚动内容
 */
@interface HotNewsTableViewCell : UITableViewCell
@property (nonatomic,strong) UILabel *newsTitleLabel;
@end

@implementation HotNewsTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self =  [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self  initSubviews];
    }
    return self;
}

-(void)initSubviews
{
    _newsTitleLabel = [[UILabel alloc] init];
    _newsTitleLabel.textColor = [UIColor blackColor];
    _newsTitleLabel.font = [UIFont systemFontOfSize:14];
    _newsTitleLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_newsTitleLabel];
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    _newsTitleLabel.frame = self.bounds;
}

@end



#import "HotNewsView.h"
@interface HotNewsView ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSTimer *timer;

@property (nonatomic,assign) CGFloat frameHeight;
@property (nonatomic,assign) int currentIndex;
@property (nonatomic,strong) NSMutableArray *dealDataArray;
@end
static NSString *hotNewsCellID = @"hotNewsCellID";
@implementation HotNewsView

-(NSMutableArray *)dealDataArray
{
    if (_dealDataArray == nil) {
        _dealDataArray = [NSMutableArray array];
    }
    return _dealDataArray;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.currentIndex = 1;
        [self initSubviews];
    }
    return self;
}

#pragma mark initSubviews
-(void)initSubviews
{
    self.backgroundColor = [UIColor orangeColor];
    _tableView = [[UITableView alloc] init];
    [_tableView registerClass:[HotNewsTableViewCell class] forCellReuseIdentifier:hotNewsCellID];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.scrollEnabled = NO;
    [self addSubview:_tableView];
    self.clipsToBounds = YES;
    _tableView.backgroundColor = [UIColor lightTextColor];
}

#pragma mark layoutSubviews
-(void)layoutSubviews
{
    [super layoutSubviews];
    self.frameHeight = self.frame.size.height;
    _tableView.frame = CGRectMake(0, 0, self.frame.size.width, self.dealDataArray.count * self.frameHeight);
}

#pragma mark tableview  数据源--代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dealDataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HotNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:hotNewsCellID forIndexPath:indexPath];
    cell.newsTitleLabel.text = self.dealDataArray[indexPath.row];
    cell.backgroundColor = [UIColor lightTextColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.frame.size.height;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger index = indexPath.row == self.dealDataArray.count-1 ? 0 : indexPath.row;
    if (self.hotNewsBlock) {
        self.hotNewsBlock(index);
    }
}


-(void)setHotNewsArray:(NSArray *)hotNewsArray
{
    _hotNewsArray = hotNewsArray;
    [self.dealDataArray addObjectsFromArray:hotNewsArray];
    [self.dealDataArray addObject:[hotNewsArray firstObject]];
    
    [_tableView reloadData];
    if (hotNewsArray.count > 1) {
        if (self.timer) {
            [self endTimer];
        }
        [self startTimer];
    }
}


#pragma mark  开始定时器
-(void)startTimer
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(scrollHotNews) userInfo:nil repeats:YES];
}
#pragma mark 结束定时器
-(void)endTimer
{
    [_timer invalidate];
    _timer = nil;
}
#pragma mark scrollHotNews
-(void)scrollHotNews
{
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = _tableView.frame;
        frame.origin.y = -(self.currentIndex * self.frameHeight);
        _tableView.frame = frame;
    } completion:^(BOOL finished) {
        
    }];
    self.currentIndex += 1;
    if (self.currentIndex == self.dealDataArray.count+1) {
        CGRect frame = _tableView.frame;
        frame.origin.y = -self.frameHeight;
        _tableView.frame = frame;
        self.currentIndex = 2;
    }
}
@end

